import datetime
import json
import random
from datetime import datetime
import mysql.connector

# 数据库信息
mydb = mysql.connector.connect(
    host="localhost",
    user="root",
    password="123456",
    database="takedrugsdata"
)


# 游标
mycursor = mydb.cursor()

# 姓氏
xingshi = ['赵', '钱', '孙', '李', '周', '吴', '郑', '王', '冯', '陈', '褚', '卫', '蒋', '沈', '韩', '杨', '朱', '秦', '尤', '许', '何', '吕',
           '施', '张', '孔', '曹', '严', '华', '金', '魏', '陶', '姜', '戚', '谢', '邹', '喻', '柏', '水', '窦', '章', '云', '苏', '潘', '葛',
           '奚', '范', '彭', '郎', '鲁', '韦', '昌', '马', '苗', '凤', '花', '方', '俞', '任', '袁', '柳', '酆', '鲍', '史', '唐', '费', '廉',
           '岑', '薛', '雷', '贺', '倪', '汤', '滕', '殷', '罗', '毕', '郝', '邬', '安', '常', '乐', '于', '时', '傅', '皮', '卞', '齐', '康',
           '伍', '余', '元', '卜', '顾', '孟', '平', '黄', '和', '穆', '萧', '尹', '姚', '邵', '湛', '汪', '祁', '毛', '禹', '狄', '米', '贝',
           '明', '臧', '计', '伏', '成', '戴', '谈', '宋', '茅', '庞', '熊', '纪', '舒', '屈', '项', '祝', '董', '梁', '杜', '阮', '闵', '贾',
           '娄', '江', '童', '颜', '郭', '梅', '盛', '林', '刁', '钟', '徐', '邱', '骆', '高', '夏', '蔡', '田', '樊', '胡', '凌', '霍', '虞',
           '万', '支', '柯', '昝', '管', '卢', '莫', '经', '房', '裘', '缪', '干', '解', '应', '宗', '丁', '宣', '贲', '邓', '郁', '单', '杭',
           '洪', '包', '诸', '左', '石', '崔', '吉', '钮', '龚']

# 常见汉字
chinese_characters = ['云', '彩', '雨', '露', '风', '花', '月', '光', '星', '晴', '雪', '冰', '玉', '翠', '金', '银', '山', '水', '石',
                      '岳', '峰', '涛', '潮', '江', '河', '湖', '海', '天', '地']

# 常见汉字组合
chinese_combinations = ['婷', '琳', '娜', '倩', '芳', '洁', '秀', '霞', '英', '丽', '妍', '梅', '雪', '莉', '静', '娟', '媛', '萍', '燕',
                        '珍', '华', '爽', '春', '夏', '秋', '冬']


# 生成随机中文名字
def chinese_name():
    name = ''
    # 随机生成两个汉字
    name += random.choice(xingshi)
    name += random.choice(chinese_characters)
    # 随机生成一个汉字组合
    name += random.choice(chinese_combinations)
    return name


# 随机生日以及年信息
def age_and_birth_date():
    young_bias = 5  # 年轻人偏差值
    age = random.randint(15, 50 + young_bias)
    if age <= 25:
        age = random.randint(15, 25)  # 年轻人年龄更有可能落在15到25岁之间
    else:
        age -= young_bias  # 中年人年龄会偏大，需要减去偏差值
    years = str(int(datetime.now().year) - age)
    birth_date = years + "-" + str(random.randint(1, 13)) + "-" + str(random.randint(1, 29))
    return age, birth_date


# 随机电话号码
def phone_number():
    # 首位为1，第2-3位为区号（随机选择）
    area_code = random.choice(['10', '21', '22', '23', '24', '25', '27', '28', '29', '311', '351', '371', '431',
                               '451', '471', '531', '551', '571', '591', '731', '771', '791', '851', '871', '891',
                               '898', '931'])
    # 剩余9位为随机数字
    number = ''.join([str(random.randint(0, 9)) for _ in range(9)])
    # 拼接成完整的13位电话号码
    return f'1{area_code}{number}'


# 随机学历
def education():
    education_levels = [
        '小学', '初中', '高中', '中专/职高', '大专', '本科', '硕士', '博士'
    ]
    weights = [10, 15, 20, 25, 30, 50, 20, 2]  # 每个学历对应的权重

    # 根据权重随机选择学历
    education = random.choices(education_levels, weights=weights)[0]
    return education


# 随机家庭背景
def family_background():
    lever = ["极好", "很好", "好", "一般", "还行", "普通", "极差", "很差", "差"]
    economic_condition = random.choice(lever)
    family_members_occupation = random.choice(lever)
    education_level = random.choice(lever)
    social_status = random.choice(lever)

    return f'<br>家庭经济状况：{economic_condition}<br>家庭成员职业：{family_members_occupation}<br>教育水平：{education_level}<br>家庭社会地位：{social_status}'


# 随机经济状况
def economic_condition():
    economic_levels = ['较差', '一般', '良好', '优秀']
    weights = [20, 50, 25, 10]  # 每个经济状况对应的权重

    # 根据权重随机选择经济状况
    economic_condition = random.choices(economic_levels, weights=weights)[0]
    return economic_condition


# 随机社交圈信息
def social_circle():
    # 定义各类社交圈的名称
    social_circles = ["球迷", "音乐爱好者", "摄影爱好者", "电影迷", "健身爱好者", "美食爱好者", "旅游爱好者", "书虫"]

    # 定义各类社交圈的比例
    social_circle_proportions = [0.2, 0.1, 0.1, 0.1, 0.1, 0.15, 0.15, 0.1]

    # 根据比例生成各类社交圈的数量
    social_circle_counts = [int(proportion * 10) for proportion in social_circle_proportions]

    # 随机生成社交圈信息
    social_circle_members = []
    for i, count in enumerate(social_circle_counts):
        for j in range(count):
            member = "编号:"+f"{i + 1}-{j + 1}"+" 姓名:"+chinese_name()+" 相识渠道:"+social_circles[i]+" 关系等级:"+random.choice(["好友", "熟人", "陌生人"])+" 不好的交集:"+random.choice(["是", "否"])

            social_circle_members.append(member)
    # 打印生成的社交圈信息
    return str("<br>".join(social_circle_members))


# 随机职业
def occupation():
    occupations = ["无业游民", "服务员", "酒保", "保安", "快递员", "清洁工"]

    random_occupation = random.choice(occupations)

    return random_occupation


# 插入100条伪数据
def insert_fake_info():
    for i in range(1000):
        # 身份证
        id_temp = [str(random.randint(0, 10)) for i in range(18)]
        id_number = "".join(id_temp)
        # 年龄和出生日期
        age, birth_date = age_and_birth_date()
        mycursor.execute(
            "insert into user (name, gender, age, birthdate, id_number, contact_info, education_background, occupation, family_background,economic_status, social_circle) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",
            (chinese_name(), str(random.randint(0, 1)), str(age), birth_date, id_number, phone_number(),
             education(), occupation(), family_background(), economic_condition(), social_circle()
             ))
        mydb.commit()

if __name__ == '__main__':

    insert_fake_info() # 插入1000条伪数据
